const http = require('../../utils/http')
const util = require('../../utils/util')

Page({
      data: {
            userData: {},
            showData: {},
            activeIndex: 0,
            weekList: ['周日', '周一', '周二', '周三', '周四', '周五', '周六',

            ],//星期列表

            showTimeModal: false,
            tabWidth: null,
            scrollX: 32,//测试点击出来的第一个tab初始left
            tabIndex: 0,
            workTime: {},
            tabList: [],
            chooseTimeIndex: [],
            chooseCurrentIndex: null,

            payType: 0,
            query: {
                  channel: 0,
                  refId: 1,
                  limitCount: '',
                  trainerId: 84,
                  timeCount: 0,
                  type: 0,
                  actualDate: '',
                  timeQuantum: {}
            },
            params: {
                  groupClassId: '',
                  channel: 0,
                  trainerId: '',
                  type: 1
            }
      },
      onLoad: function (options) {
            console.log(options, 'options--')
            wx.getStorage({
                  key: 'trainerUser',
                  success: (res) => {
                        console.log(res, 'res---')
                        if (res.data.type == 1) {
                                    this.setData({
                                          userData: res.data,
                                          'showData.price':res.data.orderPrice,
                                          'query.trainerId': res.data.userId,
                                          'query.refId': res.data.categoryId,
                                          params:{},
                                          'query.limitCount': res.data.limitCount
                                    })
                                    this.getPayOrderQuanTime()

                        } else {
                              this.setData({
                                    userData: res.data,
                                    query:{},
                                    'showData.price':res.data.orderPrice,
                                    'params.trainerId': res.data.teacherId,
                                    'params.groupClassId': res.data.id
                              })
                        }

                  },
                  fail(res) {
                        console.log(res, 'error')
                  }
            });


      },
      onShow() {

      },
      // 获取可以下单的时间列表
      getPayOrderQuanTime() {
            let that = this;
            http.request({
                  url: '/trainer/selectTimeQuantum',
                  method: 'GET',
                  data: {
                        trainerId: this.data.userData.userId || 84
                  },
                  callBack: (res) => {

                        let data = res.data;
                        // console.log(data, 'data-----')
                        let arr = data.orderTime ? data.orderTime.split(',') : [];

                        let today = new Date();
                        // 单日毫秒数
                        let oneData = 86400000;

                        // 重组tab数组
                        let m = today.getMonth() + 1;
                        let dateTime = Number(today.getDate()) < 10 ? '0' + today.getDate() : today.getDate()

                        // 推算出后面一个星期
                        let obj = {}, year = today.getFullYear();
                        // console.log(new Date((today.getTime() + oneData)))
                        arr.forEach((item, index) => {
                              arr[index] = {
                                    hour: item,
                                    select: false,
                                    disabled: false
                                    // select: false,  //选中状态
                                    // disable:false   //禁用状态
                              }
                        })

                        let jsonArr = JSON.stringify(arr)
                        let tabList = [{
                              week: '今天',
                              date: (m < 10 ? '0' + m : m) + '-' + dateTime,
                              hour: JSON.parse(jsonArr)
                        }]
                        for (let i = 1; i <= 31; i++) {
                              let time = new Date((today.getTime() + oneData * i))
                              tabList.push(Object.assign(that.getDateTime(time), {hour: JSON.parse(jsonArr)}))
                              // obj[year+'-'+time.date]
                        }
                        // console.log(tabList, 'tabList-=---')
                        //重组orderTime数组。便于选中显示状态

                        // console.log(arr, 'arr00')
                        // 重组一下已下过单的时间
                        // if (data.mapList.length > 0) {
                        // 	data.mapList.forEach(item => {
                        // 		item.actualDate = item.actualDate.substring(5);
                        // 		item.disableTimeArr = item.timeQuantum.split(',');
                        // 		// item.disableTimeArr.push(Number(item.disableTimeArr[item.disableTimeArr.length - 1]) + 1)
                        //
                        // 	})

                        // data.mapList[0].disableTimeArr.forEach((item, index) => {
                        // 	let current = arr.findIndex((current) => current.hour == item);
                        // 	if(arr[current]) arr[current].disabled = true
                        // })
                        // }


                        data.orderTime = arr;

                        //重组下过单时间的列表数组


                        this.setData({

                              workTime: data,
                              tabList: tabList,
                              'query.actualDate': today.getFullYear()
                        })
                        // console.log(tabList, 'odertime--');
                        // console.log(this.data.workTime, 'odertime--');

                  }

            })

      },
      // 获取每个Item
      getDateTime(date) {
            let m = date.getMonth() + 1
            let dateTime = Number(date.getDate()) < 10 ? '0' + date.getDate() : date.getDate()
            return {
                  week: this.data.weekList[date.getDay()],
                  date: (m < 10 ? '0' + m : m) + '-' + dateTime
            }
      },

      // 选择时间逻辑
      chooseOrderTime(e) {
            let idx = e.currentTarget.dataset.choose;
            let between;
            let tabList = this.data.tabList;


            let orderTime = tabList[this.data.activeIndex]
            // console.log(orderTime, 'orderTime---')
            let chooseIndexList = this.data.chooseTimeIndex;


            /**
             * 如果里面没有就添加
             * 如果是取消了就删除
             * **/

            orderTime.hour[idx].select = !orderTime.hour[idx].select;
            /** 如果里面没有就添加 **/

            /** 如果取消了 **/
            if (orderTime.hour[idx].select) {
                  if (chooseIndexList.indexOf(idx) == -1) {
                        chooseIndexList.push(idx)
                  }

            } else {
                  chooseIndexList.splice(chooseIndexList.indexOf(idx), 1)
            }
            /** 如果选中了 **/



            let date = new Date();
            let key = date.getFullYear() + '-' + this.data.tabList[this.data.activeIndex].date;

            let selecHour = []
            orderTime.hour.forEach(item => {
                  if (item.select) {
                        selecHour.push(item.hour)
                  }
            })

            this.data.query.timeQuantum[key + ""] = selecHour.toString() + ','


            // console.log(this.data.query.timeQuantum, 'chooseIndexList---')
            this.setData({

                  tabList: tabList,
                  chooseTimeIndex: chooseIndexList,
                  chooseCurrentIndex: idx
            })


            // let idx = e.currentTarget.dataset.choose;
            // let between;
            // // 显示的数组
            // let arr = this.data.workTime.orderTime;
            // // 记录点击的数组
            // let arr2 = this.data.chooseTimeIndex
            // arr2=Array.from(new Set(arr2))
            // //如果点击的是同一个
            //
            // let index = arr2.findIndex((item) => item == idx);
            // if(arr[idx].disabled) return
            // // 点击选中
            // if (index>-1) {
            // 	arr2.sort();
            // 	// console.log(idx-1,arr.length-1,'排序前')
            // 	// console.log(idx,'idx===')
            // 	// console.log(arr2,'选中的数组')
            // 	// console.log(arr,'显示时间的数组')
            // 	// 如果点击的不是选中的第一个和最后一个
            // 	if(idx!=arr2[0] && idx !=arr2[arr2.length-1]){
            // 		let spliceArr=arr2.splice(index,arr.length-1);
            // 		// console.log(spliceArr,'spliceArr----')
            // 		// arr[idx].select = !arr[idx].select;
            // 		//从中间选的把后面的截掉并把状态改成未选中
            // 		for(let i=0;i<spliceArr.length;i++){
            // 			// console.log(arr[i],'arr2====')
            // 			arr[spliceArr[i]].select=false;
            // 		}
            // 	}else{
            // 		// 如果是第一个或者最后一个就把状态改变并把存入选中的数组删掉
            //
            // 		arr[idx].select=!arr[idx].select
            // 		arr2.splice(index,1)
            // 		// console.log(arr2,'arr2---')
            // 	}
            //
            // } else {
            //
            // 	arr2 = arr2.concat(idx)
            // 	// 先去重
            // 	arr2 = Array.from(new Set(arr2))
            // 	arr2.sort()
            // 	// 算出数组第一个和最后一个的差额
            // 	between = arr2[arr2.length - 1] - arr2[0];
            //
            // 	//如果between小于0
            // 	if (between <= 0) {
            // 		// 说明是选中的相连的两个，改变其中一个值就可以了
            // 		arr[arr2[0]].select =   !arr[arr2[0]].select;
            // 	} else {
            // 		// i=-1   因为要把自己也删掉
            // 		for (let i = -1; i < between; i++) {
            // 			arr[arr2[0] + i+1].select = true;
            // 			// 把中间的也存入选中的数组当中
            // 			arr2=arr2.concat( arr2[0] + i+1)
            // 		}
            // 	}
            //
            // }
            //
            // this.setData({
            // 	'workTime.orderTime': arr,
            // 	chooseTimeIndex: arr2,
            // 	chooseCurrentIndex: idx
            // })
            //


      },

      // 跳转球馆
      selectStadium() {
            wx.navigateTo({
                  url: './choose-stadium/stadium'
            })
      },
      chooseTime() {
            let today = new Date();
            this.setData({
                  showTimeModal: true,
                  'query.actualDate': today.getFullYear()
            })
      },
      closeModal() {
            // 点击确定关闭
            let keys = Object.values(this.data.query.timeQuantum).toString().split(',').filter((key) => Number(key) > 0)
            // console.log(keys, 'keys---')

            // let selecHour=[]
            // // userData.orderPrice || userData.price
            //    for(let i=0;i< this.data.tabList.length;i++){
            //          for(let j=0;j<this.data.tabList[i].hour.length;j++){
            //                if(this.data.tabList[i].hour[j].select){
            //                      selecHour.push(this.data.tabList[i].hour[j].hour)
            //                }
            //          }
            //    }
            //    console.log(selecHour,'selechour----')
            this.setData({
                  activeIndex:0,
                  'query.timeCount': keys.length,
                  'userData.orderPrice': this.data.showData.price * keys.length,
                  'showData.time': keys.length,
                  showTimeModal: false
            })

      },
      getTabIndex(data) {

            let arr = this.data.workTime.orderTime;

            // let disableTime = this.data.disableTime;
            arr.forEach(item => {
                  item.select = false;

            })


            let currentDay = this.data.tabList[data.detail].date;

            // disableTime.forEach((item, index) => {
            //
            // 	if (item.actualDate == currentDay) {
            // 		item.disableTimeArr.forEach((itm, idx) => {
            // 			let current = arr.findIndex((current) => current.hour == itm);
            // 			arr[current].disabled = true
            // 		})
            // 	}
            //
            //
            // })
            this.setData({

                  'workTime.orderTime': arr,
                  activeIndex: data.detail,
                  chooseTimeIndex: [],
            })
      },
      titleClick(e) {
            console.log('titleClick')
            let index = e.currentTarget.dataset.idx;
            let that = this
            util.getDomClientRect('.swiperItem' + index).then(res => {
                  that.setData({
                        scrollX: res[0].left * 2,//换算成rpx单位
                        tabIndex: index
                  })
            })

      },
      closeModal1() {
            // 点击XX按钮
            this.setData({
                  showTimeModal: false,
            })
      },
      selectPayType(e) {
            console.log(this.data.userData.type)
            if (this.data.userData.type == 1) {
                  this.setData({
                        "query.channel": Number(e.detail.value)
                  })
            } else {
                  this.setData({
                        "params.channel": Number(e.detail.value)
                  })
            }

      },

      payOrder(type) {


            this.setData({
                  "query.timeQuantum": JSON.stringify(this.data.query.timeQuantum)
            })
            console.log(this.data.query, '-----')
            http.request({
                  url: '/ballOrder/payOrder',
                  data: this.data.query,
                  callBack: (res) => {

                        if (res.code == 200) {
                              if (type == 0) {
                                    this.requestPaymentForWX(res.data);
                              } else {
                                    wx.navigateTo({
                                          url: '/pages/order/orderIndex',
                                          success:(res)=> {
                                              wx.showToast({
                                                    title:'支付成功'
                                              })
                                          }
                                    })
                              }

                        } else {
                              this.setData({
                                    "query.timeQuantum": JSON.parse(this.data.query.timeQuantum)
                              })

                              wx.showToast({

                                    title: res.msg,
                                    icon:'none',
                                    success:(res) =>{

                                    }
                              })
                        }

                  }
            })

      },
      groupClassOrder(type) {

            http.request({
                  url: '/groupOrder/create',
                  data: this.data.params,
                  method: "POST",
                  callBack: (res) => {

                        if (type == 0) {
                              this.requestPaymentForWX(res.data);
                        } else {
                              if (res.code == 200) {
                                    wx.navigateTo({
                                          url: '/pages/order/orderIndex',
                                          success: (res) => {
                                                wx.showToast({

                                                      title: '支付成功'
                                                })
                                          }
                                    })
                              } else {

                                    wx.showToast({

                                          title: res.msg,
                                          icon: "none",
                                          success:(res)=> {

                                          }
                                    })
                              }

                        }
                  }
            })

      },
      createOrder() {

            if ((!this.data.showData.time || this.data.showData.time.length==0) && this.data.userData.type == 1) {
                  wx.showToast({
                        icon: 'none',
                        title: '请选择授课时间段'
                  })
                  return
            }
            let that = this;
            // qyyyNKno0QhOv7Mgc1Uk1qMkJQxV7WAamQ6I1zA47LA
            console.log(this.data.params,'params-----')
            console.log(this.data.query,'query-----')
            // 微信支付
            if (this.data.query.channel == 0 || this.data.params.channel == 0) {

                  wx.requestSubscribeMessage({
                        tmplIds: ['g6h1Vhd3frq2B85MLoeTLto5I_SXzoDDEesfzKvVfMw'],

                        complete() {
                              if (that.data.userData.type == 1) {
                                    that.payOrder(0)
                              } else {
                                    that.groupClassOrder(0)
                              }

                        }
                  })
            } else {
                  // 余额支付
                  wx.showModal({
                        title: '确认支付购买课程吗',
                        success: (res) => {
                              if (res.confirm) {
                                    if (this.data.userData.type == 1) {
                                          that.payOrder(1)
                                    } else {
                                          that.groupClassOrder(1)
                                    }
                              }
                        }
                  })

            }


      },
      requestPaymentForWX(data) {
            // appId: "wxfa5368fa43713400"
            // nonceStr: "1597992055202"
            // packageValue: "prepay_id=wx21144055143891c80d802bd6366c770000"
            // paySign: "BD49BA5849BF4D9DB49CF1C3D8A28420"
            // signType: "MD5"
            // timeStamp: "1597992055"
            wx.requestPayment({
                  timeStamp: data.timeStamp,
                  nonceStr: data.nonceStr,
                  package: data.packageValue,
                  paySign: data.paySign,
                  signType: data.signType,
                  success(res) {
                        wx.navigateTo({
                              url: '/pages/order/orderIndex',
                              success(res) {
                                    wx.showToast({
                                          title: ' 支付成功'
                                    })
                              },

                        })
                  },
                  fail:(res)=> {
                        console.log()
                        if(this.data.userData.type == 1){
                              this.setData({
                                    "query.timeQuantum": JSON.parse(this.data.query.timeQuantum)
                              })
                        }

                  }
            })
      }
});